package com.runa.rccprotocol.analysis.kute;

import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 2024/11/22 15:46
 * zhangYB
 */
@Data
@Slf4j
public class KtAnalysis {

    protected IInterData interData;

    public int valveHandle(byte[] data, int begin, int length) {
        System.out.println(ByteUtil.byteToHexString(data, begin, length));
        boolean v = false;
        for (int i = begin; i < length; i++) {
            if ((data[i] & 0xff) == 0x68) {
                begin = i;
                v = true;
                break;
            }
        }
        if (!v) {
            return 0;
        }
        if (length < begin + 11) {
            return -1;
        }
        int c = (data[begin + 9] & 0xff);
        switch (c) {
            case 0x81:
                return ktReadValveResponse(data, begin, length);
            case 0xA5:
                if (length < begin + 11 + 12 + 2) {
                    return -1;
                }
                if ((data[begin + 11 + 12] != ByteUtil.sum(data, begin, 11 + 12))) {
                    return 0;
                }
                this.interData = new RunaConcenAffirmDeny(true);
                break;
        }
        return 0;
    }

    private int ktReadValveResponse(byte[] data, int begin, int length) {
        int len = data[begin + 10];
        if (length < begin + 11 + len + 2) {
            return -1;
        }
        if ((data[begin + 11 + len] != ByteUtil.sum(data, begin, 11 + len))) {
            return 0;
        }
        ValveVO valveVO = new ValveVO();
        valveVO.setValveSerial(ByteUtil.byteToHexString(data, begin+1, 1) + ByteUtil.byteToHexString(data, begin+4, 5));
        int index = begin + 14;
        valveVO.setOpenTime((float)ByteUtil.byteToInt(data, index, ByteOrder.DCBA));
        index += 4;
        byte b1 = data[index++];
        byte b2 = data[index++];
        if ((b1 & 1) == 1) {
            // 管道温度1
            index += 2;
        }
        if ((b1 >> 1 & 1) == 1) {
            // 管道温度2
            index += 2;
        }
        if ((b1 >> 2 & 1) == 1) {
            // 管道压力1
            index += 2;
        }
        if ((b1 >> 3 & 1) == 1) {
            // 管道压力2
            index += 2;
        }
        if ((b2 & 1) == 1) {
            // 模块电压
            index += 1;
        }
        Date parse;
        try {
            parse = new SimpleDateFormat("yyMMddHHmmss").parse(ByteUtil.byteToHexString(data, index, 6));
            valveVO.setValveDate(parse);
        } catch (ParseException e) {

        }
        index += 6;
        valveVO.setStatus(ByteUtil.byteToHexString(data, index, 2));
        byte v1 = data[index++];
        byte v2 = data[index++];
        if (v1 == 0xCC) {
            valveVO.setActOpen(100f);
        } else if (v1 == 0xDD) {
            valveVO.setActOpen(0f);
        } else {
            valveVO.setActOpen((float)v1);
        }
        List<String> faultList = new ArrayList<>();
        if ((v2 >> 1 & 1) == 1 || (v2 >> 2 & 1) == 1) {
            faultList.add("温度异常");
        }
        if ((v2 >> 3 & 1) == 1) {
            faultList.add("阀门异常");
        }
        valveVO.setFaultCode(faultList);

        RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
        runaConcenResponseData.addRepData(valveVO);
        this.interData = runaConcenResponseData;
        return 0;
    }



}
